package cac.fenjian.resource.mspartbasicbao.rest;

import cac.fenjian.resource.MsPart.dto.MsPartBasicDTO;
import cac.fenjian.resource.mspartbasicbao.service.MsPartService;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@Api(tags = "resource", description = "product_code")
@RequestMapping("/msPartBasic/")
public class MsPartBasicRest {

    @Autowired
    private MsPartService msPartBasicService;

    /*
    条件查询
     */
    @GetMapping("/search/s1/{ms_part_id}")
    @ApiOperation(value = "条件查询")

    public ResponseMsg<MsPartBasicDTO> search(@ApiParam(value = "机型", name = "msPartId") @PathVariable("ms_part_id") String msPartId) {
        ResponseMsg<MsPartBasicDTO> responseMsg = new ResponseMsg<>();
        MsPartBasicDTO msPartBasicDTO = msPartBasicService.searchByProductCode(msPartId);
        if (msPartBasicDTO != null) {
            responseMsg.setResponseBody(msPartBasicDTO);
        }
        return responseMsg;
    }


    @PostMapping("/searchByPage/s1")
    @ApiOperation(value = "按条件分页查询")

    public ResponseMsg<QueryRespBean<MsPartBasicDTO>> searchByPage(@ApiParam(value = "查询条件", name = "queryReqBean") @RequestBody QueryReqBean<MsPartBasicDTO> queryReqBean) {
        ResponseMsg<QueryRespBean<MsPartBasicDTO>> responseMsg = new ResponseMsg<>();
        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), MsPartBasicDTO[].class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            MsPartBasicDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<MsPartBasicDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<MsPartBasicDTO> result = msPartBasicService.searchMsPartBasicByPage(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    @PostMapping("/save/v1")
    @ApiOperation(value = "新增对象")
    public ResponseMsg<String> save(@ApiParam(value = "保存对象", name = "msPartBasic") @RequestBody MsPartBasicDTO msPartBasicDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msPartBasicService.insertMsPartBasic(msPartBasicDTO));
        return responseMsg;
    }

    @PostMapping("/save/v2")
    @ApiOperation(value = "批量新增")
    public ResponseMsg<Integer> save2(@ApiParam(value = "批量新增", name = "msPartBasicBatch") @RequestBody List<MsPartBasicDTO> msPartBasicDTOList) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msPartBasicService.insertMsPartBasic1List(msPartBasicDTOList));
        return responseMsg;
    }

    @PostMapping("/update/v1")
    @ApiOperation(value = "修改部分字段")

    public ResponseMsg<Integer> updateMsPartBasic(@ApiParam(value = "修改对象", name = "updateMsPartBasic") @RequestBody MsPartBasicDTO msPartBasicDTO) throws ParseException {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msPartBasicService.updateMsPartBasic(msPartBasicDTO));
        return responseMsg;
    }

    @PostMapping("/update/v2")
    @ApiOperation(value = "批量更新")

    public ResponseMsg<Integer> updateMsPartBasic2(@ApiParam(value = "修改对象", name = "updateMsPartBasicBatch") @RequestBody List<MsPartBasicDTO> msPartBasicDTO) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msPartBasicService.updateMsPartBasicList2(msPartBasicDTO));
        return responseMsg;
    }

    @PostMapping("/delete/v1")
    @ApiOperation(value = "按主键单条删除")

    public ResponseMsg<Integer> deleteById(@ApiParam(value = "主键id", name = "msPartId") @RequestBody String msPartId) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msPartBasicService.deleteMsPartBasic(msPartId));
        return responseMsg;
    }


    @PostMapping("/deleteByIds/v1")
    @ApiOperation(value = "批量删除")

    public ResponseMsg<Integer> deleteByIds(@ApiParam(value = "逗号分隔的id串", name = "ids") @RequestBody String[] ids) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(msPartBasicService.deleteMsPartBasicByIds(ids));
        return responseMsg;
    }

}
